<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>【运维】5、Docker运行SpringCloud微服务并通过IP进行服务注册 | 亦人亦码</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="近期在做项目的容器化改造，公司提供了一整套容器化管理平台，在正式上之前，现在本地搭建了一套测试环境（Centos7+Docker）去跑一下服务，测试dockerfiler，eureka注册，服务调用等。">
<meta property="og:type" content="article">
<meta property="og:title" content="【运维】5、Docker运行SpringCloud微服务并通过IP进行服务注册">
<meta property="og:url" content="https://xlh_blog.gitee.io/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%916%E3%80%81Docker%E8%BF%90%E8%A1%8CSpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%B9%B6%E9%80%9A%E8%BF%87IP%E8%BF%9B%E8%A1%8C%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C/index.html">
<meta property="og:site_name" content="亦人亦码">
<meta property="og:description" content="近期在做项目的容器化改造，公司提供了一整套容器化管理平台，在正式上之前，现在本地搭建了一套测试环境（Centos7+Docker）去跑一下服务，测试dockerfiler，eureka注册，服务调用等。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-06T07:35:28.000Z">
<meta property="article:modified_time" content="2023-04-06T07:44:34.623Z">
<meta property="article:author" content="Mr.xi">
<meta property="article:tag" content="Java, PHP,Linux,博客">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="亦人亦码" type="application/atom+xml">
  
  
    <link rel="icon" href="/image/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">


<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">亦人亦码</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
          <a class="main-nav-link" href="/about">About</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://xlh_blog.gitee.io"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-【运维】6、Docker运行SpringCloud微服务并通过IP进行服务注册" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%916%E3%80%81Docker%E8%BF%90%E8%A1%8CSpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%B9%B6%E9%80%9A%E8%BF%87IP%E8%BF%9B%E8%A1%8C%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C/" class="article-date">
  <time datetime="2023-04-06T07:35:28.000Z" itemprop="datePublished">2023-04-06</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E8%BF%90%E7%BB%B4/">运维</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      【运维】5、Docker运行SpringCloud微服务并通过IP进行服务注册
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>近期在做项目的容器化改造，公司提供了一整套容器化管理平台，在正式上之前，现在本地搭建了一套测试环境（Centos7+Docker）去跑一下服务，测试dockerfiler，eureka注册，服务调用等。</p>
<span id="more"></span>

<p>测试环境：Centos7*2[192.168.56.101，192.168.56.102]+Docker</p>
<p>系统：SpringCloud、SpringBoot2.x、Eureka</p>
<h1 id="1、配置并启动Eureka-Server"><a href="#1、配置并启动Eureka-Server" class="headerlink" title="1、配置并启动Eureka Server"></a>1、配置并启动Eureka Server</h1><p>Eureka服务端配置如下：[运行在Eureka192.168.56.101]</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">port</span>: <span class="string">8011</span></span><br><span class="line"></span><br><span class="line"><span class="attr">eureka</span>:<span class="string"></span></span><br><span class="line">  <span class="attr">instance</span>:<span class="string"></span></span><br><span class="line">    <span class="attr">hostname</span>: <span class="string">localhost</span></span><br><span class="line">  <span class="attr">client</span>:<span class="string"></span></span><br><span class="line">    <span class="meta">register-with-eureka</span>: <span class="string">false</span></span><br><span class="line">    <span class="meta">fetch-registry</span>: <span class="string">false</span></span><br><span class="line">    <span class="meta">enable-self-preservation</span>: <span class="string">false</span></span><br><span class="line">    <span class="attr">serviceUrl</span>:<span class="string"></span></span><br><span class="line">      <span class="attr">defaultZone</span>: <span class="string">http://$&#123;eureka.instance.hostname&#125;:$&#123;server.port&#125;/eureka/</span></span><br></pre></td></tr></table></figure>

<p>编辑dockerfile：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">基础dockerfile地址，新项目不要更改</span></span><br><span class="line">FROM java:8</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">dockerfile owner 新项目请变更为 项目名/owner</span></span><br><span class="line">MAINTAINER  projectname/owner</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">ADD 项目包名到容器的 /data/xxx路径下</span></span><br><span class="line">ADD server-0.0.1.jar /data/xxx/</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 应用端口</span></span><br><span class="line">EXPOSE 8011</span><br><span class="line"></span><br><span class="line">ENV TZ=Asia/Shanghai</span><br><span class="line">RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/timezone</span><br><span class="line">RUN mkdir /data/logs</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">切换工作目录</span></span><br><span class="line">WORKDIR /data/xxx</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">启动目录</span></span><br><span class="line">ENTRYPOINT   [&quot;java&quot;,&quot;-jar&quot;,&quot;server-0.0.1.jar&quot;]</span><br></pre></td></tr></table></figure>

<p>构建镜像：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f server-8001 -t server:0.0.1 .</span><br></pre></td></tr></table></figure>

<p>运行容器：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8001:8001 --name=server --hostname=server server:0.0.1</span><br></pre></td></tr></table></figure>

<h1 id="2、配置并启动Eureka-Client"><a href="#2、配置并启动Eureka-Client" class="headerlink" title="2、配置并启动Eureka Client"></a>2、配置并启动Eureka Client</h1><p>Eureka 客户端配置如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">eureka:</span><br><span class="line">  client:</span><br><span class="line">    fetch-registry: true</span><br><span class="line">    register-with-eureka: true</span><br><span class="line">    serviceUrl:</span><br><span class="line">      defaultZone: http://192.168.56.102:8011/eureka/</span><br><span class="line">  instance:</span><br><span class="line">    prefer-ip-address: true #使用IP进行注册</span><br><span class="line">    ip-address: $&#123;HOST&#125; # 使用固定IP</span><br></pre></td></tr></table></figure>

<p>客户端注册的时候采用的是IP注册的方式，因为Docker运行微服务，进行注册的时候，如果采用实例ID注册，然么在进行服务调用时最后在封装请求的时候获取的是Docker容器的IP，比如172.17.0.1。这时候在通过OpenFeign进行服务调用的时候就会失败。</p>
<p>编辑dockerfile：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">基础dockerfile地址，新项目不要更改</span></span><br><span class="line">FROM java:8</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">dockerfile owner 新项目请变更为 项目名/owner</span></span><br><span class="line">MAINTAINER  projectName/owner</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">ADD 项目包名到容器的 /data/gwapp路径下</span></span><br><span class="line">ADD projectname-0.0.1-SNAPSHOT.jar /data/xxx/</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 应用端口</span></span><br><span class="line">EXPOSE 8014</span><br><span class="line"></span><br><span class="line">ENV TZ=Asia/Shanghai</span><br><span class="line">RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/timezone</span><br><span class="line"></span><br><span class="line">RUN mkdir /data/logs</span><br><span class="line"><span class="meta">#</span><span class="bash">切换工作目录</span></span><br><span class="line">WORKDIR /data/xxx/</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">启动目录</span></span><br><span class="line">ENTRYPOINT   [&quot;java&quot;,&quot;-jar&quot;,&quot;xxx-0.0.1-SNAPSHOT.jar&quot;]</span><br></pre></td></tr></table></figure>

<p>然后以容器化运行客户端时如何将“HOST”传递进去呢？</p>
<p><strong>方式一：</strong></p>
<p>直接在docker run的时候传递变量，如下：</p>
<p>首先构建镜像：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -f xxx -t xxx:0.0.1 .</span><br></pre></td></tr></table></figure>

<p>然后运行容器，并设置HOST参数：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -e HOST=&#x27;192.168.56.101&#x27; -p 8014:8014 --name=xxx --restart=always --hostname=xxx xxx:0.0.1</span><br></pre></td></tr></table></figure>

<p><strong>方式二：在dockerfile中写死</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash">基础dockerfile地址，新项目不要更改</span></span><br><span class="line">FROM java:8</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">dockerfile owner 新项目请变更为 项目名/owner</span></span><br><span class="line">MAINTAINER  projectName/xxx</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">ADD 项目包名到容器的 /data/gwapp路径下</span></span><br><span class="line">ADD xxx-0.0.1.jar /data/xxx/</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 应用端口</span></span><br><span class="line">EXPOSE 8011</span><br><span class="line"></span><br><span class="line">ENV TZ=Asia/Shanghai</span><br><span class="line">RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/timezone</span><br><span class="line">RUN mkdir /data/logs</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">切换工作目录</span></span><br><span class="line">WORKDIR /data/xx</span><br><span class="line"><span class="meta"></span></span><br><span class="line"><span class="meta">#</span><span class="bash">启动目录</span></span><br><span class="line">ENTRYPOINT   [&quot;java&quot;,&quot;-DHOST=192.168.56.101&quot;,&quot;-jar&quot;,&quot;xxx-0.0.1.jar&quot;]</span><br></pre></td></tr></table></figure>

<p>至此就可以实现客户端注册，然后服务间访问调用的时候，可以获取到宿主机的IP，而不是服务所在容器的IP。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://xlh_blog.gitee.io/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%916%E3%80%81Docker%E8%BF%90%E8%A1%8CSpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%B9%B6%E9%80%9A%E8%BF%87IP%E8%BF%9B%E8%A1%8C%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C/" data-id="clt2kwbit0010s337ajx2ch6z" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%917%E3%80%81%E8%A7%A3%E5%86%B3virtualBox%E5%AF%BC%E5%85%A5voa%E6%96%87%E4%BB%B6%E5%90%8E%E6%97%A0%E6%B3%95%E4%B8%8A%E7%BD%91%E9%97%AE%E9%A2%98/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          【运维】7、解决virtualBox导入voa文件后无法上网问题
        
      </div>
    </a>
  
  
    <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%915%E3%80%81win10%E9%80%9A%E8%BF%87vagrant-virtulbox%E6%90%AD%E5%BB%BAcentos7%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%97%B6%E6%8A%A5%E9%94%99/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">【运维】5、win10通过vagrant+virtulbox搭建centos7虚拟机环境时报错</div>
    </a>
  
</nav>

  
</article>




</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Activiti7/">Activiti7</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Git/">Git</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%85%B6%E4%BB%96/">其他</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BF%90%E7%BB%B4/">运维</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/04/">四月 2023</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/04/10/%E3%80%90%E5%85%B6%E4%BB%96%E3%80%911%E3%80%81Mac%E9%83%A8%E7%BD%B2LskyPro%E6%90%AD%E5%BB%BA%E6%9C%AC%E5%9C%B0%E5%9B%BE%E5%BA%8A/">【其他】1、Mac部署LskyPro搭建本地图床</a>
          </li>
        
          <li>
            <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%917%E3%80%81%E8%A7%A3%E5%86%B3virtualBox%E5%AF%BC%E5%85%A5voa%E6%96%87%E4%BB%B6%E5%90%8E%E6%97%A0%E6%B3%95%E4%B8%8A%E7%BD%91%E9%97%AE%E9%A2%98/">【运维】7、解决virtualBox导入voa文件后无法上网问题</a>
          </li>
        
          <li>
            <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%916%E3%80%81Docker%E8%BF%90%E8%A1%8CSpringCloud%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%B9%B6%E9%80%9A%E8%BF%87IP%E8%BF%9B%E8%A1%8C%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C/">【运维】5、Docker运行SpringCloud微服务并通过IP进行服务注册</a>
          </li>
        
          <li>
            <a href="/2023/04/06/%E3%80%90%E8%BF%90%E7%BB%B4%E3%80%915%E3%80%81win10%E9%80%9A%E8%BF%87vagrant-virtulbox%E6%90%AD%E5%BB%BAcentos7%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%97%B6%E6%8A%A5%E9%94%99/">【运维】5、win10通过vagrant+virtulbox搭建centos7虚拟机环境时报错</a>
          </li>
        
          <li>
            <a href="/2023/04/06/%E3%80%90%E5%90%8E%E7%AB%AF%E3%80%91SpringBoot%E6%95%B4%E5%90%88MQTT%EF%BC%88EMQX%EF%BC%89/">【后端】SpringBoot整合MQTT（EMQX）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2024 Mr.xi<br>
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
        <span id="busuanzi_container_site_pv">
          本文总阅读量<span id="busuanzi_value_page_pv"></span>次
        </span><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备18049089号-2</a>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>


</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>





<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?803c71743b7cfc4dac421ac4fc646963";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



  </div>
</body>
</html>